package com.sphd.education.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.sphd.education.R;
import com.sphd.education.activitys.SchoolMsgsActivity;
import com.sphd.education.activitys.SystemMsgsActivity;
import com.sphd.education.info.DelInfo;
import com.sphd.education.info.MsgInfo;
import com.sphd.education.utils.ToastUtils;

import org.litepal.crud.DataSupport;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hu on 2016/2/2.
 */
public class SystemMsgAdapter extends BaseAdapter {
    Context context;
    List<MsgInfo> msgInfos;
    // 用来控制CheckBox的选中状况
    private static HashMap<Integer, Boolean> isSelected;
    Boolean showing=false;
    public SystemMsgAdapter(Context context, List<MsgInfo> msgInfos) {
        this.context = context;
        this.msgInfos = msgInfos;
        isSelected = new HashMap<Integer, Boolean>();
        // 初始化数据
        initDate();
    }
    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < msgInfos.size(); i++) {
            getIsSelected().put(i, false);
        }
    }
    @Override
    public int getCount() {
        return msgInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.system_msg_item, null);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        viewHolder.tchTitle.setText(msgInfos.get(position).getTitle());
        viewHolder.tchTime.setText(new SimpleDateFormat("MM月dd日").format(new Date(msgInfos.get(position).getCreatTime())));
        viewHolder.sysCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context, SystemMsgsActivity.class);
                intent.putExtra("sysmsg", (Serializable) msgInfos.get(position));
                context.startActivity(intent);
            }
        });
        if (showing){
            viewHolder.systemCheck.setVisibility(View.VISIBLE);
            // 根据isSelected来设置checkbox的选中状况
            viewHolder.systemCheck.setChecked(getIsSelected().get(position));
        }else {
            viewHolder.systemCheck.setVisibility(View.GONE);
            initDate();
        }
        viewHolder.systemCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DelInfo delInfo = new DelInfo();
                if (!isSelected.get(position)){
                    isSelected.put(position, true);
                    setIsSelected(isSelected);
                    delInfo.setDelId(msgInfos.get(position).getId());
                    delInfo.save();
                }else {
                    isSelected.put(position, false);
                    setIsSelected(isSelected);
                    DataSupport.deleteAll(DelInfo.class, "delId=?", msgInfos.get(position).getId()+"");
                    delInfo.save();
                }
            }
        });
        return convertView;
    }
    public void setShow(boolean sorc){
        showing=sorc;
    }
    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

        public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        SystemMsgAdapter.isSelected = isSelected;
    }
    static class ViewHolder {
        @Bind(R.id.tch_title)
        TextView tchTitle;
        @Bind(R.id.tch_time)
        TextView tchTime;
        @Bind(R.id.sys_card)
        CardView sysCard;
        @Bind(R.id.system_del_checkbox)
        CheckBox systemCheck;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
